var SceneVideoFusion = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.globe = options.editor.globe;
	this.sceneId=options.sceneId;
	this.editDIndex;
	this.model=null;
	this.isOpen = false;
}

SceneVideoFusion.prototype.open = function() {
	var _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/videoFusion/library", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.closeAll();
	layer.open({
		id: 'vmodelLibrary',
		title: '视频融合组件',
		type: 1,
		area: '220px',
		content: res,
		shade: 0,
		offset: 'r',
		fixed: true,
		move: false,
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpen = true;
			_this.dIndex = dIndex;
			$(layero).children('.layui-layer-content').css('overflow', 'hidden');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 40px)');
			form.render();
			
			upload.render({
				elem: '#scene-vmodel'
				, url: _this.ctx + '/scene/videoFusion/upload?sceneId=' + _this.sceneId
				, accept: 'file'
				,exts:'zip'	
				,size:4096
				, before: function(obj) {
					layer.load();
				}
				, done: function(res, index, upload) {
					layer.closeAll('loading'); //关闭loading
					if (200 == res.code) {
						layer.msg(res.message, { icon: 1 });
						let options=res.data;
						let options1=$.extend(true, {}, options);
						options.uri=_this.ctx;
				    	_this.drawVModel=_this.globe.drawVModel(options);
				    	_this.drawVModel.on('success',function(position){
				    		if(position){
				                var formData={}; 
				                formData['sceneVmodel.id']=options.id;
				                formData['sceneVmodel.x']=position.x;
				                formData['sceneVmodel.y']=position.y;
				                formData['sceneVmodel.z']=position.z;
				                layer.load(2);
				    			var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/videoFusion/save",formData, "json", null);
				    			layer.closeAll('loading');
				    			if(200==res.code){
				    				layer.msg(res?res.message:'系统异常!', {icon: 1});
				    				options1.position=position;
				        			_this.globe.render(options1,_this.ctx);
				        			//_this.edit(res.data.id) 
				    			}else{
				    				 layer.msg(res?res.message:'系统异常!', {icon: 2});
				    			}
				    		}
				    	});
				    	_this.drawVModel.on('error',function(e){
				    		 common_ajax.ajaxFunc('get', _this.ctx + "/scene/videoFusion/delete",{id:options.id}, "json", null);
				    	});
				    	_this.drawVModel.on('complete',function(e){
				    	});
					} else {
						layer.msg(res.message, { icon: 2 });
					}
				}
				, error: function(index, upload) {
					layer.closeAll('loading');
					layer.msg("上传失败！", { icon: 2 });
				}
			});
			
			//位置拾取
			$("#vmodelPickPositionBtn").on('click', function() {
				if(!$(this).hasClass("scene-btn-disabled")){
					$(this).addClass("scene-btn-disabled");
				}else{
					$(this).removeClass("scene-btn-disabled");
					_this.createEditTool(_this.model);
				}
			})
			
			
		},
		end: function() {
			_this.isOpen = false;
			_this.dIndex=null;
			_this.model=null;
		}
	});
}

SceneVideoFusion.prototype.editVModel = function(obj) {
	var _this = this;
	_this.model=obj;
	if (_this.isOpenEditVModel) {
		_this.closeEditPModelTool();
		_this.renderVModelDataForm(_this.model);
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/videoFusion/edit", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.open({
		id:'editVModel',
		type: 1,
		offset: '40px',
		title: '编辑',
		area: '300px',
		resize: false,
		shade: 0,
		offset: 'l',
		fixed: true,
		move: false,
		content: res,
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpenEditVModel=true;
			_this.editEditVModel=dIndex;
			$(layero).children('.layui-layer-content').css('overflow', 'visible');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 40px)');
			_this.renderVModelDataForm(_this.model);
			_this.renderForm();
		},
		end: function() {
			_this.isOpenEditVModel=false;
			_this.editEditVModel=null;
			_this.closeEditPModelTool();
		}
	});
}

/**
 * 渲染表单
 */
SceneVideoFusion.prototype.renderForm=function(){
	var _this = this;
	upload.render({
		elem: '#vmodelUploadVideoBtn'
		, url: _this.ctx + '/scene/videoFusion/uploadVideo?id=' + _this.model.id
		, accept: 'file'
		,size:1024*100
		, before: function(obj) {
			layer.load();
		}
		, done: function(res, index, upload) {
			layer.closeAll('loading'); //关闭loading
			if (200 == res.code) {
				layer.msg(res.message, { icon: 1 });
				form.val('vmodelForm', res.data);
				if(model){
					 model.updateThing(res.data);
					 _this.updateEditVModelTool(model);
				 }
				_this.webRtcSwitch();
			} else {
				layer.msg(res.message, { icon: 2 });
			}
		}
		, error: function(index, upload) {
			layer.closeAll('loading');
			layer.msg("上传失败！", { icon: 2 });
		}
	});
	
	upload.render({
		elem: '#vmodelExchangeBtn'
		, url: _this.ctx + '/scene/videoFusion/replace?id=' +  _this.model.id
		, accept: 'file'
		,exts:'zip'	
		,size:4096
		, before: function(obj) {
			layer.load();
		}
		, done: function(res, index, upload) {
			layer.closeAll('loading'); //关闭loading
			if (200 == res.code) {
				layer.msg(res.message, { icon: 1 });
				form.val('vmodelForm', res.data);
				$("#vmodel-thumbnail-img").attr("src",_this.ctx + res.data.thumbnail);
			} else {
				layer.msg(res.message, { icon: 2 });
			}
		}
		, error: function(index, upload) {
			layer.closeAll('loading');
			layer.msg("上传失败！", { icon: 2 });
		}
	});
	
	form.on('select(videoType)', function(data){
		_this.webRtcSwitch(data.value);
	});
	
	$('#vmodel-scaleX-input').on('input', function() {
		 if(_this.model && $("#vmodel-scaleX-input").val()*1 != 0){
			 _this.model.updateScaleX($("#vmodel-scaleX-input").val());
			 _this.updateEditVModelTool(model);
		 }
	 });
	
	$('#vmodel-scaleY-input').on('input', function() {
		 if(_this.model && $("#vmodel-scaleY-input").val()*1 != 0){
			 _this.model.updateScaleY($("#vmodel-scaleY-input").val());
			 _this.updateEditVModelTool(model);
		 }
	 });

	$('#vmodel-scaleZ-input').on('input', function() {
		 if(_this.model && $("#vmodel-scaleZ-input").val()*1 != 0){
			 _this.model.updateScaleZ($("#vmodel-scaleZ-input").val());
			 _this.updateEditVModelTool(model);
		 }
	 });

	
	$('#vmodel-heading-input').on('input', function() {
		 if(_this.model){
			 _this.model.updateHeading($("#vmodel-heading-input").val());
		 }
	 });
	
	$('#vmodel-pitch-input').on('input', function() {
		 if(_this.model){
			 _this.model.updatePitch($("#vmodel-pitch-input").val());
		 }
	 });	 
	 
	$('#vmodel-roll-input').on('input', function() {
		 if(_this.model){
			 _this.model.updateRoll($("#vmodel-roll-input").val());
		 }
	 });	
	
	$('#vmodel-z-input').on('input', function() {
		 if(_this.model){
			 _this.model.updatePosition({
				 x:$("#vmodel-x-input").val()*1,
				 y:$("#vmodel-y-input").val()*1,
				 z:$("#vmodel-z-input").val()*1,
			 });
			 _this.updateEditVModelTool(model);
		 }
	 });
	
	$('#vmodelPositionBtn').click(function() {
		_this.globe.flyToPoisition(data);
	});
	
	$('#vmodelDeleteBtn').click(function() {
		layer.confirm('确定要删除模型吗？', {
			shade: .1
		}, function(index) {
			_this.delete(model.id);
		}, function(index) {
			layer.close(index);
			return false;
		});
	});
	
	form.on('submit(vmodelSubmitForm)', function(data) {
		layer.load(2);
		var formData = {};
		for (var key in data.field) {
			if ('file' !== key)
				formData['sceneVmodel.' + key] = data.field[key];
		}
		var r = common_ajax.ajaxFunc('post', _this.ctx + "/scene/videoFusion/save", formData, "json", null);
		layer.closeAll('loading');
		if(r && 200==r.code){
			layer.msg(r.message, { icon: 1 });
		}else{
			layer.msg(r.message, { icon: 2 });
		}
		return false;
	});
	form.render();
}

/**
 * 加载数据
 */
SceneVideoFusion.prototype.renderVModelDataForm=function(obj){
	let data={};
	var _this = this;
	if(_this.model){
		data={
			id:obj.id,
			name:obj.name||'',
			code:obj.code||'',
			
			x:obj.position.x||'',
			y:obj.position.y||'',
			z:obj.position.z||0,
			
			heading:obj.orientation.heading||0,
			pitch:obj.orientation.pitch||0,
			roll:obj.orientation.roll||0,
			
			scaleX:obj.model.scaleX||1,
			scaleY:obj.model.scaleY||1,
			scaleZ:obj.model.scaleZ||1,
			isRelative:obj.model.isRelative||'0',
			videoRelative:obj.model.videoRelative||'0',
			videoType:obj.model.videoType||'0',
			videoUrl:this.getVideoUrl(obj),
			webRTCUrl:obj.model.webRTCUrl||'',
			videoId:obj.model.videoId||'',
			thumbnail:this.getThumbnailUrl(obj),
			url:obj.model.url||1,
		}	
	}
	_this.createEditVModelTool(obj)
	form.val('vmodelForm', data);
	_this.webRtcSwitch();
	if(data && data.thumbnail){
		$("#vmodel-thumbnail-img").attr("src",_this.ctx + data.thumbnail);
	}
	
	return data;
}

SceneVideoFusion.prototype.getVideoUrl = function(obj) {
	if("0" !=obj.model.videoRelative && obj.model.videoUrl && obj.model.videoUrl.indexOf(this.ctx) != -1){
		return obj.model.videoUrl.substring(this.ctx.length,obj.model.videoUrl.length)
	}
	return obj.model.videoUrl;
}

SceneVideoFusion.prototype.getThumbnailUrl = function(obj) {
	if(obj.model.thumbnail && obj.model.thumbnail.indexOf(this.ctx) != -1){
		return obj.model.thumbnail.substring(this.ctx.length,obj.model.thumbnail.length)
	}
	return obj.model.thumbnail;
}


SceneVideoFusion.prototype.delete = function(id) {
	var _this = this;
	layer.confirm('确定要删除模型吗？', {
		shade: .1
	}, function(index) {
		layer.close(index);
		layer.load(2);
		var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/videoFusion/delete", { id: id }, "json", null);
		layer.closeAll('loading');
		if (200 == res.code) {
			layer.msg(res.message, { icon: 1 });
			layer.close(_this.editEditVModel);
			_this.globe.remove(id);
			return true;
		} else {
			layer.msg(res.message, { icon: 2 });
			return false;
		}
	}, function(index) {
		layer.close(index);
		return false;
	});
}

SceneVideoFusion.prototype.webRtcSwitch = function(type) {
	var _this = this;
	if(!type){
		var data = form.val('vmodelForm');
		type = data.videoType;
	}
	if('1'==type){
		$("#vmodel-webRTCUrl-input-tr").css('display','');
		$("#vmodel-videoId-input-tr").css('display','');
	}else {
		$("#vmodel-webRTCUrl-input-tr").css('display','none');
		$("#vmodel-videoId-input-tr").css('display','none');
	}
}

SceneVideoFusion.prototype.createEditVModelTool=function(model){
	let _this=this;
	_this.editVModelTool=_this.globe.createEditVModel(model);
	_this.editVModelTool.on('change',function(e){
		if(e.heading){
			$("#vmodel-heading-input").val(e.heading)
		}
		if(e.position){
			$("#vmodel-x-input").val(e.position.x)
			$("#vmodel-y-input").val(e.position.y)
			$("#vmodel-z-input").val(e.position.z)
		}
	});
}

SceneVideoFusion.prototype.updateEditVModelTool=function(model){
	let _this=this;
	if(_this.editVModelTool){
		_this.editVModelTool.update(model);
	}
}

SceneVideoFusion.prototype.closeEditPModelTool=function(){
	let _this=this;
	if(_this.editVModelTool){
		_this.editVModelTool.destroy();
		_this.editVModelTool=null;
	}
}

SceneVideoFusion.prototype.close = function(){
	var _this = this;
	if(_this.editEditVModel){
		layer.close(_this.editEditVModel);	
	}
}
